
package com.jf.cloud.service.sysmanager.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.base.GenericsUtils;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.db.Condition;
import com.jf.cloud.service.sysmanager.common.db.jdbc.RowMapperFactory;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.dao.IPubgMsgLogService;
import com.jf.cloud.service.sysmanager.entity.PubgMsgLog;
import com.jf.cloud.service.sysmanager.entity.PubgUser;
import com.jf.cloud.service.sysmanager.entity.PubgUserMatch;
import com.jf.cloud.service.sysmanager.entity.PubgUserTeam;


/**
 * 员工服务实现
 * 
 * @author 绝峰代码机1.0 创建时间：2018-03-31
 */
@Service("pubgMsgLogService")
public class PubgMsgLogServiceImpl extends BaseService<PubgMsgLog, Long> implements IPubgMsgLogService {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgMsgLogServiceImpl.class);

	/**
	 * JPA公共DAO
	 */
	@Resource
	private BaseDao baseDao;

	private RowMapper<PubgMsgLog> rowMapper = new BeanPropertyRowMapper<PubgMsgLog>(GenericsUtils.getSuperClassGenricType(this.getClass()));

	/**
	 * 根据主键查询员工
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	public PubgMsgLog findPubgMsgLog(Long rowId) throws Exception {
		try {
			return this.baseDao.findObject("", rowMapper, rowId);
		} catch (Exception e) {
			throw new Exception("根据主键查询员工出错！", e);
		}
	}

	/**
	 * 根据查询条件获取员工列表
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	public List<PubgMsgLog> findPubgMsgLogList(Condition qc) throws Exception {
		try {
			return this.baseDao.findList("", rowMapper);
		} catch (Exception e) {
			throw new Exception("根据查询条件获取员工列表出错！", e);
		}
	}

	/**
	 * 新增/修改员工
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	@Transactional
	public PubgMsgLog saveOrUpdatePubgMsgLog(PubgMsgLog pubgMsgLog) throws Exception {
		try {
			return this.baseDao.saveAll(pubgMsgLog);
		} catch (Exception e) {
			throw new Exception("新增/修改员工出错！", e);
		}
	}

	/**
	 * 插入队员消息
	 * @param msgType 10 通知消息      20 公告
	 * 
	 * @author 朱建文 创建时间：2018年4月3日 下午9:20:11
	 */
	@Transactional
	public void insertPbugMsgLog(Long userId, Long msgType,String smallName, String content){
		try {
			PubgMsgLog pubgMsgLog = new PubgMsgLog();
			pubgMsgLog.setUserId(userId);
			pubgMsgLog.setMsgType(msgType);
			pubgMsgLog.setSmallName(smallName);
			pubgMsgLog.setContent(content);
			pubgMsgLog.setCreateTime(DateUtil.getCurrentDateTime());
			
			// 查询队员信息
			String sql = "SELECT * FROM pubg_user WHERE ROW_ID = ?";
			PubgUser pubgUser = this.baseDao.findObject(sql, RowMapperFactory.newInstance(PubgUser.class), userId);
			if (pubgUser != null) {
				pubgMsgLog.setUserAccount(pubgUser.getUserAccount());
			}
			
			// 查询队员-战队关系
			sql = "SELECT * FROM pubg_user_team WHERE USER_ID = ? AND RELATION_STATE = 3";
			List<PubgUserTeam> pubgUserTeamList = this.baseDao.findList(sql, RowMapperFactory.newInstance(PubgUserTeam.class), userId);
			if (pubgUserTeamList != null && !pubgUserTeamList.isEmpty()) {
				pubgMsgLog.setClubId(pubgUserTeamList.get(0).getClubId());
				pubgMsgLog.setClubName(pubgUserTeamList.get(0).getClubName());
				pubgMsgLog.setTeamId(pubgMsgLog.getTeamId());
				pubgMsgLog.setTeamName(pubgMsgLog.getTeamName());
			}
			this.baseDao.saveAll(pubgMsgLog);
		} catch (DataAccessException e) {
			log.error("插入队员消息出错！", e);
		}
	}
	
	/**
	 * 插入队员通知（因赛事状态改变）
	 * 
	 * @author 朱建文 创建时间：2018年4月26日 下午1:13:28
	 */
	@Override
	@Transactional
	public boolean insertPbugMsgLog(Long matchId, String smallName, String content) {
		boolean boo = false;
		try {
			String sql = "SELECT CLUB_ID, CLUB_NAME, TEAM_ID, TEAM_NAME, USER_ID, USER_ACCOUNT FROM pubg_user_match ";
			sql += " WHERE MATCH_ID = ? AND USER_STATE = 20 ";
			List<PubgUserMatch> dataList = this.baseDao.findList(sql, 
					RowMapperFactory.newInstance(PubgUserMatch.class), matchId);
			
			PubgMsgLog pubgMsgLog = null;
			for (PubgUserMatch pubgUserMatch : dataList) {
				pubgMsgLog = new PubgMsgLog();
				pubgMsgLog.setUserId(pubgUserMatch.getUserId());
				pubgMsgLog.setUserAccount(pubgUserMatch.getUserAccount());
				pubgMsgLog.setMsgType(10L);
				pubgMsgLog.setSmallName(smallName);
				pubgMsgLog.setContent(content);
				pubgMsgLog.setCreateTime(DateUtil.getCurrentDateTime());
				pubgMsgLog.setClubId(pubgUserMatch.getClubId());
				pubgMsgLog.setClubName(pubgUserMatch.getClubName());
				pubgMsgLog.setTeamId(pubgUserMatch.getTeamId());
				pubgMsgLog.setTeamName(pubgUserMatch.getTeamName());
				this.baseDao.saveAll(pubgMsgLog);
			}
			boo = true;
		} catch (Exception e) {
			log.error("插入队员消息（因赛事状态改变）出错！", e);
		}	
		return boo;
	}
}
